﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace HDJ.Framework.Core
{
    public class StateGUIInspector<S, T> where S : StateDataBase, new() where T : StateTransitionDataBase, new()
    {

        public static void OnHeaderGUI(StateGUIData<S> state)
        {
            if (state == null)
                return;

            if (state.stateData == null)
                return;

            EditorGUIUtility.SetIconSize(Vector2.one * 45);
            GUIContent cc = EditorGUIUtility.IconContent("GameManager Icon");
            cc.text = "";
            GUILayout.BeginHorizontal("GroupBox");
            //  GUILayout.Space(5);
            GUILayout.Label(cc);

            GUILayout.BeginVertical();
            GUILayout.Space(2);
            GUILayout.Label("ID : " + state.stateData.id);
            GUILayout.Space(2);
            state.stateData.name = EditorGUILayout.TextArea(state.stateData.name);
            GUILayout.EndVertical();
            GUILayout.EndHorizontal();
            GUILayout.Space(10);
            OnInspectorGUI(state);
        }
        static CustomStateGUIBase eInstance = null;
        private static void OnInspectorGUI(StateGUIData<S> state)
        {
            if (state == null)
                return;

            if (state.stateData == null)
                return;
            GUILayout.BeginVertical("box");
            foreach (var item in state.stateData.transitionToStateIDs)
            {

                T t = SM_EditorController<S, T>.Instance.machineData.GetTransition(item);
                if (t == null)
                    continue;
                S fromState = SM_EditorController<S, T>.Instance.machineData.GetState(t.fromStateID);
                S toState = SM_EditorController<S, T>.Instance.machineData.GetState(t.toStateID);
                GUILayout.Label(fromState.name + " -> " + toState.name, "box");
            }
            GUILayout.EndVertical();
            GUILayout.Space(5);
            if (eInstance == null)
                eInstance = (CustomStateGUIBase)EditorExtendAttributeUtils.GetEditorExtend(typeof(CustomStateGUIBase), typeof(S));
            if (eInstance == null)
            {
                eInstance = new CustomStateGUIBase();
            }
            if (eInstance != null)
            {
                eInstance.target = state;

                eInstance.OnInspectorGUI();
            }
        }
    }
}
